//
//  jymimaViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/21.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "jymimaViewController.h"
#import "Masonry.h"
#import "SVProgressHUD.h"
#import "GPosAPI.h"
#import "WKProgressHUD.h"
#import "XMRegisterViewController.h"

#define ALPHA @"0123456789"

@interface jymimaViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField * onemimaTixeField;
@property (nonatomic,strong)UITextField * twomimaTixeField;
@property (nonatomic,strong)UIButton * oneyanButton;
@property (nonatomic,strong)UIButton * twoyanButton;
@property (nonatomic,strong)UIButton * querenButton;
@property (nonatomic,strong)WKProgressHUD *hun;
@end

@implementation jymimaViewController
//改变状态栏
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
//    self.title = @"设置交易密码";
    self.navigationItem.title=@"设置交易密码";
    //隐藏返回提示文字
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel * toptishiLabel = [[UILabel alloc] init];
    toptishiLabel.text = @"您尚未设置交易密码，请设置交易密码保障账户安全";
    toptishiLabel.textAlignment=NSTextAlignmentCenter;
    toptishiLabel.textColor = [UIColor blueColor];
    toptishiLabel.font = [UIFont systemFontOfSize:14];
    toptishiLabel.numberOfLines = 2;
    [self.view addSubview:toptishiLabel];
    
    [toptishiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(kNavbarHeight+25);
        make.left.equalTo(self.view).with.offset(25);
        make.right.equalTo(self.view).with.offset(-25);
        make.centerX.equalTo(self.view);
    }];

    UIImageView * onesuoImage = [[UIImageView alloc] init];
    onesuoImage.image = [UIImage imageNamed:@"icon_mima_l"];
    [self.view addSubview:onesuoImage];
    
    [onesuoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(toptishiLabel.mas_bottom).with.offset(25);
        make.left.equalTo(self.view).with.offset(25);
        
    }];

    self.oneyanButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.oneyanButton setImage:[UIImage imageNamed:@"icon_biyan_l"] forState:UIControlStateNormal];
    [self.oneyanButton addTarget:self action:@selector(oneyanButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.oneyanButton];
    
    [self.oneyanButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(onesuoImage);
        make.right.equalTo(self.view).with.offset(-25);
        
    }];
    
    self.onemimaTixeField = [[UITextField alloc] init];
    self.onemimaTixeField.placeholder =@"请设置6位交易密码";
    self.onemimaTixeField.keyboardType=UIKeyboardTypeNumberPad;
    self.onemimaTixeField.font = [UIFont systemFontOfSize:14.f];
    self.onemimaTixeField.secureTextEntry = YES;
    self.onemimaTixeField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.onemimaTixeField];
    [self.onemimaTixeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(onesuoImage);
        make.left.equalTo(onesuoImage.mas_right).with.offset(20);
        make.width.equalTo(@200);
    }];
    
    UIView * oneView = [[UIView alloc] init];
    oneView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:oneView];
    [oneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(onesuoImage.mas_bottom).with.offset(5);
        make.left.equalTo(self.view).with.offset(25);
        make.right.equalTo(self.view).with.offset(-25);
        make.height.equalTo(@1);
    }];
    
    UIImageView * twosuoImage = [[UIImageView alloc] init];
    twosuoImage.image = [UIImage imageNamed:@"icon_mima_l"];
    [self.view addSubview:twosuoImage];
    
    [twosuoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(oneView.mas_bottom).with.offset(25);
        make.left.equalTo(self.view).with.offset(25);
        
    }];
    
    self.twoyanButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.twoyanButton setImage:[UIImage imageNamed:@"icon_biyan_l"] forState:UIControlStateNormal];
    [self.twoyanButton addTarget:self action:@selector(twoyanButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.twoyanButton];
    
    [self.twoyanButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(twosuoImage);
        make.right.equalTo(self.view).with.offset(-25);
        
    }];
    
    self.twomimaTixeField = [[UITextField alloc] init];
    self.twomimaTixeField.placeholder =@"请再次输入";
    self.twomimaTixeField.keyboardType=UIKeyboardTypeNumberPad;
    self.twomimaTixeField.font = [UIFont systemFontOfSize:14.f];
    self.twomimaTixeField.secureTextEntry = YES;
    self.twomimaTixeField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.twomimaTixeField];
    [self.twomimaTixeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(twosuoImage);
        make.left.equalTo(twosuoImage.mas_right).with.offset(20);
        make.width.equalTo(@200);
    }];
    
    UIView * twoView = [[UIView alloc] init];
    twoView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:twoView];
    [twoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(twosuoImage.mas_bottom).with.offset(5);
        make.left.equalTo(self.view).with.offset(25);
        make.right.equalTo(self.view).with.offset(-25);
        make.height.equalTo(@1);
    }];
    
    self.querenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.querenButton setBackgroundImage:[UIImage imageNamed:@"icon_anniu_f"] forState:UIControlStateNormal];
    [self.querenButton setTitle:@"确认" forState:UIControlStateNormal];
    [self.querenButton addTarget:self action:@selector(querenButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.querenButton];
    
    
    [self.querenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(twoView.mas_bottom).with.offset(30);
        
    }];
    self.onemimaTixeField.delegate = self;
    self.twomimaTixeField.delegate = self;
}

//眼睛
- (void)oneyanButtonAction:(UIButton *)sender {
    if (self.onemimaTixeField.secureTextEntry == YES) {
        
        self.onemimaTixeField.secureTextEntry = NO;
        [self.oneyanButton setImage:[UIImage imageNamed:@"icon_yangjingl"] forState:UIControlStateNormal];
    }else {
        self.onemimaTixeField.secureTextEntry = YES;
        [self.self.oneyanButton setImage:[UIImage imageNamed:@"icon_biyan_l"] forState:UIControlStateNormal];
        
    }
    
    
    
}
- (void)twoyanButtonAction:(UIButton *)sender {
    if (self.twomimaTixeField.secureTextEntry == YES) {
        
        self.twomimaTixeField.secureTextEntry = NO;
        [self.twoyanButton setImage:[UIImage imageNamed:@"icon_yangjingl"] forState:UIControlStateNormal];
    }else {
        self.twomimaTixeField.secureTextEntry = YES;
        [self.self.twoyanButton setImage:[UIImage imageNamed:@"icon_biyan_l"] forState:UIControlStateNormal];
        
    }
    
    
    
}

//确认
- (void)querenButtonAction:(UIButton *)sender {
    
    if (self.onemimaTixeField.text.length == 0) {
        self.hun=[WKProgressHUD popMessage:@"交易密码不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.twomimaTixeField.text.length == 0) {
        self.hun=[WKProgressHUD popMessage:@"再次确认交易密码不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    
    if (self.onemimaTixeField.text.length != 6) {
        self.hun=[WKProgressHUD popMessage:@"请输入交易密码6位字符" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.twomimaTixeField.text.length != self.onemimaTixeField.text.length) {
        self.hun=[WKProgressHUD popMessage:@"再次确认密码不相同" inView:self.view duration:1 animated:YES];
        return;
    }
    
    //确认交易密码
    int str = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    //        NSString * str = [[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"]] intValue];
    NSDictionary * params = @{@"userId" : @(str),
                              @"firstPassword" : self.onemimaTixeField.text,
                              @"secondPassword" : self.twomimaTixeField.text
                              };
    self.querenButton.userInteractionEnabled=NO;
    [GPosAPI requestTradePasswordWithParams:params andCallback:^(id obj) {
        NSLog(@"shuju--%@",obj);
        if ( obj == nil )
        {
            self.querenButton.userInteractionEnabled=YES;
           self.hun=[WKProgressHUD popMessage:@"请求失败" inView:self.view duration:1 animated:YES];
            
        }else{
            
            if ([[obj objectForKey:@"code"] integerValue] == 200)
            {
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"status"];
                self.hun=[WKProgressHUD popMessage:[obj objectForKey:@"设置交易密码成功"] inView:self.view duration:1 animated:YES];
                [self.navigationController popViewControllerAnimated:YES];
                self.querenButton.userInteractionEnabled=YES;
                
               
            }else if([[obj objectForKey:@"code"] integerValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402){
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else {
                
                self.hun=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
                self.querenButton.userInteractionEnabled=YES;
               
            }
            
        }
        
    }];
    
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.onemimaTixeField ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.onemimaTixeField.text.length >= 6) {
            self.onemimaTixeField.text = [textField.text substringToIndex:6];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHA] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
    }
    
    if (textField == self.twomimaTixeField ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.twomimaTixeField.text.length >= 6) {
            self.twomimaTixeField.text = [textField.text substringToIndex:6];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHA] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
    }
    
    return YES;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
